package com.oceansoft.mobile.econsole.modules.security.controller;

import com.oceansoft.mobile.econsole.exam.entity.ExamTest;
import com.oceansoft.mobile.econsole.exam.util.QuestionToDb;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.security.Utils.ExcelUtil;
import com.oceansoft.mobile.econsole.modules.security.entity.Baykscj;
import com.oceansoft.mobile.econsole.modules.security.service.securityService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by zeng on 2016/8/30.
 */
@Controller
@RequestMapping("security")
public class SecurityController extends BaseController {

    @Resource
    private securityService securityService;

    /**
     * 保安员考试成绩首页
     * @param query
     * @param model
     * @return
     */
    @RequestMapping(value = "index")
    public String gotoSecurityIndex(Query query, Model model) {
        Pager<Baykscj> page = securityService.findSecurity(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", page);
        log("查看保安员考试成绩列表");
        return "security/security-list";
    }

    /**
     * 导入保安员考试成绩页面
     */
    @RequestMapping(value = "import")
    public String importSecurity(Model model) {
        return "security/security-import";
    }

    /**
     * 保存保安员考试成绩
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveImportExamScore", method = POST)
    public Result saveImportExamScore(MultipartHttpServletRequest request) throws IOException {
        MultipartFile requestFile = request.getFile("examScore");
        try {
            InputStream inputStream =  requestFile.getInputStream();
            ExcelUtil excelReader = new ExcelUtil();
            Map<Integer, String> map = excelReader.readExcelContent(inputStream);

            List<Baykscj> baykscjList = new ArrayList<>();
            for (int i = 1; i <= map.size(); i++) {
                String str = map.get(i);
                String[] strArray = str.split(",");

                Baykscj baykscj = new Baykscj();
                String guid = UUID.randomUUID().toString().replace("-","");
                baykscj.setGuid(guid);
                baykscj.setZkzh(strArray[0]);
                baykscj.setSfzh(strArray[1]);
                baykscj.setXm(strArray[2]);
                baykscj.setDw(strArray[3]);
                baykscj.setCj(strArray[4]);

                baykscjList.add(baykscj);
            }
            securityService.saveBaykscj(baykscjList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result();
    }
}
